#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

/*
input the directory and return the list (ventor) of all file .cpp / .h
*/

int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
string filecpp("cpp"),strcpp,strExt;
string fileh("h"),strh;

if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening directory " << dir << endl;
return errno;
}

while ((dirp = readdir(dp)) != NULL) {
// checking file .cpp or .h and push into the vector
 

strExt = string(dirp->d_name);  // get file finame
if (strExt.size()>=3){
   strcpp = strExt.substr((strExt.size()-3),3); // get file .cpp

   strh = strExt.substr(strExt.size()-1,1);    // get file .h
 
   if (strcpp.compare(filecpp)==0)             // is file .cpp, adding into
       files.push_back(string(dirp->d_name));

   if (strh.compare(fileh)==0)             // is file .h, adding into
       files.push_back(string(dirp->d_name));

}
}
closedir(dp);
return 0;
}

int main()
{
// or we can declare like string dir = string("C:\\");
// string dir = string("C:\\Users\\CUONGTRAN\\Desktop\\CSUEB WINTER 2011\\CS 4311\\Add-in\\Listfile\\Listfile"); 
string dir = string(".");
vector<string> files = vector<string>();

getdir(dir,files);

for (unsigned int i = 0;i < files.size();i++) {
cout << files[i] << endl;
}
system("pause");
return 0;
}
